﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using Yuv;

namespace YuvCompare
{
  /// <summary>
  /// Window1.xaml에 대한 상호 작용 논리
  /// </summary>
  public partial class Window1 : Window
  {

    DispatcherTimer timeClock;
    YuvReader yuvReader;

    int imageWidth, imageHeight;

    public Window1()
    {
      InitializeComponent();          
    }

  
    private void DragDrop(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.FileDrop))
      {
        // Note that you can have more than one file.
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

        if (files.Length != 1)
        {
          MessageBox.Show("Not support");
          return;
        }



        //////////////////////////////////////////////////////////////////
        // test code
        // to be removed            
        String[] SubStr = files[0].Split('x');
        String[] WidthList = SubStr[0].Split('_');
        String[] HeightList = SubStr[1].Split('_', '.');
        int.TryParse(WidthList[WidthList.Length - 1], out imageWidth);
        int.TryParse(HeightList[0], out imageHeight);

        yuvReader  = new YuvReader(YUVType.FOUR_TWO_ZERO, imageWidth, imageHeight);
        

        yuvReader.open(files[0]);
        startTimer();
        ///////////////////////////////////////////////////////////////////
      }
    }



    //------------------------------------------------------------------    
    // Timer setting
    //------------------------------------------------------------------
    
    private void startTimer()
    {
      timeClock = new DispatcherTimer();
      timeClock.Interval = new TimeSpan(0, 0, 0, 0, 30);
      timeClock.IsEnabled = true;
      timeClock.Tick += new EventHandler(tick);
    }

    void tick(object sender, EventArgs e)
    {
      try
      {
        yuvReader.readFrame();
        
        LEFT_IMAGE.Source = yuvReader.getBitmap();
      }
      catch (Exception err)
      {
        timeClock.Stop();
        return;
      }
    } 
  }
}
